package com.yunqiao.api.controller;

import com.yunqiao.api.annotation.Login;
import com.yunqiao.common.exception.CustomApiException;
import com.yunqiao.common.utils.ApiResponse;
import com.yunqiao.common.utils.MapToolsUtils;
import com.yunqiao.common.utils.PageUtils;
import com.yunqiao.common.validator.ValidatorUtils;
import com.yunqiao.service.api.UsualAddressService;
import com.yunqiao.service.api.entity.UsualAddressEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.Map;


/**
 * 常用地址
 *
 * @author Crocutax
 * @email wangxw725@163.com
 * @date 2019-03-03 21:58:56
 */
@RestController
@RequestMapping("api/usualaddress")
@Api(tags = "UsualAddressApiController", description = "常用地址管理")
public class UsualAddressApiController {

    @Autowired
    private UsualAddressService usualAddressService;

    /**
     * 列表
     */
    @Login
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "long"),
        @ApiImplicitParam(name = "page", value = "页码", required = false, defaultValue = "1", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "limit", value = "数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @GetMapping("/list")
    @ApiOperation("查询常用地址 列表")
    public ApiResponse<PageUtils<UsualAddressEntity>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageUtils page = usualAddressService.queryPage(params);
        return new ApiResponse<>(page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("查询单个常用地址")
    public ApiResponse<UsualAddressEntity> info(@PathVariable("id") Long id) {
        if (id == null) {
            throw new CustomApiException("ID不能为空");
        }
        UsualAddressEntity usualAddress = usualAddressService.getById(id);
        return new ApiResponse<>(usualAddress);
    }

    /**
     * 保存
     */
    @Login
    @PostMapping("/save")
    @ApiOperation("保存常用地址")
    public ApiResponse save(@RequestBody UsualAddressEntity usualAddress) {
        ValidatorUtils.validateEntity(usualAddress);
        usualAddressService.save(usualAddress);
        return new ApiResponse<>();
    }

    /**
     * 修改
     */
    @Login
    @PutMapping("/update/{id}")
    @ApiOperation("修改常用地址")
    public ApiResponse update(@PathVariable Long id,@RequestBody UsualAddressEntity usualAddress) {
        UsualAddressEntity dbItem = usualAddressService.getById(id);
        if (dbItem == null) {
            throw new CustomApiException("ID有误, 未找到对应数据");
        }
        usualAddressService.updateById(usualAddress);
        return new ApiResponse<>();
    }

    /**
     * 删除
     */
    @Login
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除常用地址")
    public ApiResponse delete(@PathVariable Long id) {
        if (id == null) {
            throw new CustomApiException("ID不能为空");
        }
        usualAddressService.removeById(id);
        return new ApiResponse<>();
    }
    @GetMapping("/getlocation/{address}")
    @ApiOperation("查询单个常用地址")
    public ApiResponse<Map<String,Object>> getlocation(@PathVariable("address") String address) {
        if (address == null) {
            throw new CustomApiException("address不能为空");
        }
        Map<String,Object> result =new HashMap<>();
        String city = MapToolsUtils.getAddress(address);
        result.put("city",city);
        return new ApiResponse<>(result);
    }

}
